import {Injectable} from "@angular/core";
import {LogUtil} from "./log-util";
import {Observable, Subject} from "rxjs";
import {Page, Path} from "../portal/admin/admin-breadcrumb/breadcrumb";
import {HomePage} from "../constants/constants";

/**
 *  队列服务
 */
@Injectable()
export class PageSubject {

  private pageSubject: Subject<any>;

  constructor(private logUtil: LogUtil) {
    this.pageSubject = new Subject();
  }

  /**
   * 推送当前页面显示的面包屑信息
   * @param curPageName
   * @param curOperName
   * @param paths
   */
  pushPage(curPageName:string,curOperName:string, ...paths: Path[]) {
    this.logUtil.debug("paths value before filter:" + JSON.stringify(paths));

    paths = paths.filter(function(path){
      if(!!path){
        return true;
      }
      return false;
    });
    this.logUtil.debug("paths value after filter:" + JSON.stringify(paths));
    let page = new Page(HomePage.name,HomePage.url,curPageName,curOperName,!!paths ? paths : null);
    this.push(page);
  }
  private push(page: Page){
    this.logUtil.debug("添加Page成功:" + JSON.stringify(page));
    this.pageSubject.next(page);
  }

  subscribePage(): Observable<Page> {
    this.logUtil.debug("查询Page");
    return this.pageSubject.asObservable();
  }

}
